#include "Editor.hpp"

#include "Utils.hpp"


Editor::Editor(sf::RenderWindow* pRWindow, const std::string& fileName)
    :m_renderer(pRWindow),m_fileName(fileName),m_running(true)
{
}

bool Editor::load()
{
    bool bResult = true;
    Library library;
    library.init();

    m_ids = library.getIDs();

    bResult &= m_loader.load(m_fileName);

    // Chargement des sprites
    std::vector<std::string> paths = library.getLibraryPaths();
    for(unsigned int i = 0; i < paths.size() ; i++)
    {
        bResult &= m_spriteManager.loadSprite(paths[i], 15, 25, true);
    }

    m_spriteManager.generateBucket();

    //m_renderer.getApp()->SetFramerateLimit(60);

    return bResult;
}

void Editor::run()
{
    sf::Clock Clock;
    sf::String Text;
    while ( m_running )
    {
        m_renderer.clear();

        m_renderer.drawBg();
        m_renderer.drawLevel(m_loader.getLevel(m_fileName), m_spriteManager);

        m_renderer.refresh();

        handleEvents();
    }
}

void Editor::handleEvents()
{
    sf::Event Event;
    const sf::Input& Input = m_renderer.getApp()->GetInput();

    while (m_renderer.getApp()->GetEvent(Event))
    {
        // Window closed
        if (Event.Type == sf::Event::Closed)
        {
            exitGame();
        }

        // Escape key pressed
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
        {
            exitGame();
        }

        int mouseX = Input.GetMouseX();
        int mouseY = Input.GetMouseY();
        bool mouseClickedLeft = Input.IsMouseButtonDown(sf::Mouse::Left);
        bool mouseClickedRight = Input.IsMouseButtonDown(sf::Mouse::Right);
        if ( mouseClickedLeft || mouseClickedRight )
        {
            int floor = Utils::distance(mouseX,mouseY,512,384) / 50;
            int angle = Utils::RanToDeg(atan2f(mouseX-512,mouseY-384)) + 180;

            if ( floor >= 0 && floor <= 6 )
            {
                floor = 6-floor;
                angle += 90;
                angle = Utils::normaliseAngle(angle);

                angle /= (360.f / m_loader.getLevel(m_fileName).angleCount(floor));

                int actualId = m_loader.getLevel(m_fileName).getTile(floor,angle);

                int i = 0;
                for ( i = 0 ; i < m_ids.size() ; i++ )
                {
                    if ( m_ids[i] == actualId )
                    {
                        break;
                    }
                }


                if ( mouseClickedLeft )
                {
                    if ( i < m_ids.size()-1 )
                    {
                        m_loader.setLevelTile(m_fileName,floor,angle,m_ids[i+1]);
                    }
                }
                else if ( mouseClickedRight )
                {
                    if ( i == 0 )
                    {
                        // m_loader.setLevelTile(m_fileName,floor,angle,m_ids[m_ids.size()-1]);
                    }
                    else
                    {
                        m_loader.setLevelTile(m_fileName,floor,angle,m_ids[i-1]);
                    }
                }
            }
        }
    }
}

void Editor::exitGame()
{
    m_loader.getLevel(m_fileName).save(m_fileName);

    m_renderer.getApp()->Close();
    m_running = false;
}
